package com.qian.service.ddj.serviceImpl;


import com.qian.api.ddj.service.IMailService;
import com.qian.api.ddj.util.EmailUtil;
import com.qian.service.ddj.dao.OperatorLogRepository;
import com.qian.service.ddj.dao.UserRepository;
import com.qian.service.ddj.entity.DdjOperatorLog;
import com.qian.service.ddj.entity.DdjUser;
import com.yaowk.api.common.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

@Service
public class MailService implements IMailService {

    @Autowired
    private UserRepository dao;
    @Autowired
    private OperatorLogRepository operatorLogDao;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private EmailUtil emailUtil;

    private String template = "<p>\n" +
            "</p>\n" +
            "<p>\n" +
            "    尊敬的<strong style=\"white-space: normal;\">%s</strong>：\n" +
            "</p>\n" +
            "<p>\n" +
            "    &nbsp; &nbsp; &nbsp; &nbsp;您好，这里是飞付支付安全中心。\n" +
            "</p>\n" +
            "<p>\n" +
            "    &nbsp; &nbsp; &nbsp;\n" +
            "</p>\n" +
            "<p>\n" +
            "    &nbsp; &nbsp; &nbsp; &nbsp;您的验证码：<strong><span style=\"color:#ff0000;font-size:18px\">%s</span></strong>\n" +
            "</p>\n" +
            "<p>\n" +
            "    <br/>\n" +
            "</p>\n" +
            "<hr/>\n" +
            "<p>\n" +
            "</p>";


    public String geneerateCode(){
        String chars = "abcdefghijklmnopqrstuvwxyz";
       String code = String.valueOf(chars.charAt((int)(Math.random() * 26)))+String.valueOf(chars.charAt((int)(Math.random() * 26)))
               +String.valueOf(chars.charAt((int)(Math.random() * 26)))+String.valueOf(chars.charAt((int)(Math.random() * 26)))
               +String.valueOf(chars.charAt((int)(Math.random() * 26)))+String.valueOf(chars.charAt((int)(Math.random() * 26)));


        return code;
    }

    @Override
    public void forgotPassWordEmail(String userName) {
       DdjUser ddjUser = dao.findByUsername(userName);
        if (!ObjectUtils.isEmpty(ddjUser)) {
          String code = geneerateCode();

            emailUtil.bingEmail(ddjUser.getEmail(), String.format(template, ddjUser.getUsername(), code), "找回密码-全宇支付");
            stringRedisTemplate.opsForValue().set(code,userName);

      }
    }

    @Override
    public void bindEmail(String address, String username) {

        String code = geneerateCode();

        emailUtil.bingEmail(address, String.format(template, username, code), "绑定邮箱-全宇支付");
        stringRedisTemplate.opsForValue().set(code,address);
    }

    @Override
    public void checkCode(String code,String userName) {
        DdjUser ddjUser = dao.findByUsername(userName);
        if (!ObjectUtils.isEmpty(ddjUser)) {
            String value = stringRedisTemplate.opsForValue().get(code.toLowerCase());
            if (!ObjectUtils.isEmpty(value)) {
                if (StringUtils.isEmpty(ddjUser.getEmail())) {
                    ddjUser.setEmail(value);
                    dao.save(ddjUser);
                } else {
                    throw new BaseException("不能重复绑定，请联系管理员");
                }
                return;
            }
        }
        throw new BaseException("验证码验证错误");

    }

    @Override
    public void forgotPassWotdCheckCode(String userName,String password, String code) {
        String rediscode = stringRedisTemplate.opsForValue().get(code.toLowerCase());
        if(rediscode.equals(userName)){
            DdjUser ddjUser = dao.findByUsername(userName);
            ddjUser.setPassword(password);
            dao.save(ddjUser);

            DdjOperatorLog ddjOperatorLog = new DdjOperatorLog();
            ddjOperatorLog.setOperator("找回密码");
            ddjOperatorLog.setChangeId(ddjUser.getId());
            ddjOperatorLog.setChangeName(userName);

            operatorLogDao.save(ddjOperatorLog);
        }
    }
}


